//
//  LoginViewController.m
//  Kids
//
//  Created by tengfei on 14/11/22.
//  Copyright (c) 2014年 90's Technology Co.Ltd. All rights reserved.
//

#import "LoginViewController.h"
#import "FindPasswordViewController.h"
#import "HomePageViewController.h"
#import "UIColor+Extend.h"
#import "LoginView.h"
#import "RegisterView.h"

#import "RegisterRequestData.h"
#import "RegisterResponseData.h"
#import "LoginRequestData.h"
#import "LoginResponseData.h"
#import "RequestFactory.h"

#import "NSString+md5.h"
#import  "SVProgressHUD.h"

#import "AccountInfo.h"

@interface LoginViewController () <LoginViewDelegate, RegisterViewDelegate>

@property (nonatomic, strong) UIImageView* topImageView;
@property (nonatomic, strong) UIImageView* titleImageView;

@property (nonatomic, strong) UIButton* registerButton;
@property (nonatomic, strong) UIButton* loginButton;

@property (nonatomic,assign) BOOL showNumber;
@end

@implementation LoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorFDD646];
    
    UITapGestureRecognizer * tapGes =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
   
    [self.view addGestureRecognizer:tapGes];
    
    //标志图片
    self.topImageView = ({
        UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chips"]];
        [imageView setFrame:CGRectMake(135, 76, 50, 61)];
        imageView;
    });
    [self.view addSubview:self.topImageView];
    //标题图片
    self.titleImageView = ({

        UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_chipskids"]];
        [imageView setFrame:CGRectMake(0, 0, 226, 35)];
        imageView.center        = CGPointMake(CGRectGetWidth(self.view.frame)/2, 200);
        imageView;
    });
    [self.view addSubview:self.titleImageView];

    //注册按钮
    self.registerButton = ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(64, 310, 192, 46)];
        [button setBackgroundImage:[UIImage imageNamed:@"login_signin"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"login_signin_press"] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor colorFDD646] forState:UIControlStateNormal];
        [button setTitle:@"注册" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(registerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.view addSubview:self.registerButton];

    //登陆按钮
    self.loginButton = ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(64, 410, 192, 46)];
        [button setBackgroundImage:[UIImage imageNamed:@"login_signin"] forState:UIControlStateNormal];
         [button setBackgroundImage:[UIImage imageNamed:@"login_signin_press"] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor colorFDD646] forState:UIControlStateNormal];
        [button setTitle:@"登陆" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    
    
    [self.view addSubview:self.loginButton];

    self.loginView = [[LoginView alloc] init];
    self.loginView.delegate = self;

    self.registerView = [[RegisterView alloc] init];
    self.registerView.delegate = self;
    
    if([[UIScreen mainScreen] bounds].size.height == 480){
        
        self.topImageView.frame = CGRectMake(135, 40, 50, 61);
        self.titleImageView.frame = CGRectMake(0, 0, 226, 35);
        self.titleImageView.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 200);
        self.registerButton.frame = CGRectMake(64, 250, 192, 46);
        self.loginButton.frame = CGRectMake(64, 350, 192, 46);
        
        self.registerView.originRect = CGRectMake(21, 200, 278, 188);
        self.loginView.originRect =  CGRectMake(21, 220, 278, 188);
    }else{
        self.topImageView.frame = CGRectMake(135, 76, 50, 61);
        self.titleImageView.frame = CGRectMake(0, 0, 226, 35);
        self.titleImageView.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 200);
        self.registerButton.frame = CGRectMake(64, 310, 192, 46);
        self.loginButton.frame = CGRectMake(64, 410, 192, 46);

        self.registerView.originRect = CGRectMake(21, 280, 278, 188);
        self.loginView.originRect =  CGRectMake(21, 300, 278, 188);
    }
}
-(void)tapClick:(UITapGestureRecognizer *)tapGes{
    [self.loginView removeFromSuperview];
    [self.registerView removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark -
- (void)loginButtonClick:(UIButton*)button
{
    [self.registerView removeFromSuperview];
    self.showNumber = 0;
    [self.view addSubview:self.loginView];
}
- (void)registerButtonClick:(UIButton*)button
{
    [self.loginView removeFromSuperview];
    self.showNumber = 1;
    [self.view addSubview:self.registerView];
}

-(void)forgetPasswordClick{

    FindPasswordViewController * controller = [[FindPasswordViewController alloc]init];
    UINavigationController * nvc = [[UINavigationController alloc]initWithRootViewController:controller];
    [controller.navigationController.navigationBar setTintColor:[UIColor colorFDD646]];
    [self presentViewController:nvc animated:YES completion:^{
        
    }];
}
#pragma mark -
#pragma mark login view delegate

- (void)loginClick:(UIButton*)loginButton phone:(NSString*)phone password:(NSString*)password
{
    
    LoginRequestData* request = [[LoginRequestData alloc] init];
    request.phone = phone;
    request.password = [password md5];

    LoginResponseData* response = [[LoginResponseData alloc] init];
    [RequestFactory requestWithRequestData:request forResponseData:response complete:^(ResponseData* responseData, NSError* error) {
        LoginResponseData * response = (LoginResponseData *)responseData;
        if(response.result ==1){
            
            [[AccountInfo shareInstance] setLoginState:LoginStateOnline];
            [[AccountInfo shareInstance] setPhone:phone];
            [[AccountInfo shareInstance] setPassword:password];
            
            [self jumpToHomeViewController];
        }
        else {
            [SVProgressHUD showErrorWithStatus:response.info];
        }
    }];
}

#pragma mark regiester view delegate
- (void)registerClick:(UIButton*)registerButton phone:(NSString*)phone password:(NSString*)password code:(NSString*)code
{
    RegisterRequestData *request = [[RegisterRequestData alloc] init];
    request.phone = phone;
    request.pwd = [password md5];
    request.vericode = code;
    
    RegisterResponseData *response = [[RegisterResponseData alloc] init];
    [RequestFactory requestWithRequestData:request forResponseData:response complete:^(ResponseData *responseData, NSError *error) {
        RegisterResponseData *response = (RegisterResponseData *)responseData;
        if (response.result == 1) {
            
            [[AccountInfo shareInstance] setLoginState:LoginStateOnline];
            [[AccountInfo shareInstance] setPhone:phone];
            [[AccountInfo shareInstance] setPassword:password];
            
            [self jumpToHomeViewController];
        }
        else {
            [SVProgressHUD showImage:nil status:response.info];
        }
    }];
}
-(void)jumpToHomeViewController{
    UIStoryboard * storeBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    
    UINavigationController * controller = [storeBoard instantiateViewControllerWithIdentifier:@"mainViewController"];

    
    [UIApplication sharedApplication].keyWindow.rootViewController = controller;
}
@end
